package com.kits.store.ui


import android.content.ContentValues
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import com.kits.store.R
import com.kits.store.ui.kits.AudioStorage
import com.kits.store.ui.kits.FileStorage
import com.kits.store.ui.kits.ImageStorage
import kotlinx.android.synthetic.main.fragment_public_storage.*
import me.yokeyword.fragmentation.SupportFragment

class PublicStorageFragment : SupportFragment(){

    companion object{
        fun newInstance(): PublicStorageFragment{
            val args = Bundle()

            val fragment = PublicStorageFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_public_storage, container, false)
    }

    override fun onResume() {
        super.onResume()
        initView()
    }


    private fun initView(){
        btnImageStore.setOnClickListener {
            ImageStorage().standImageDirFile(requireContext())
        }

        btnFileStore.setOnClickListener {
            //FileStorage().standFile(requireContext())
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                FileStorage().saveFile(requireContext())
            }
        }

        btnAudioStore.setOnClickListener {
            //AudioStorage().standFile(requireContext())
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                println("更新文件？？？")
                AudioStorage().updateFile(requireContext(),"first.mp3")
            }
            // 查询文件
            AudioStorage().queryFile(requireContext(),"first.mp3")
        }

    }


    private fun writeNotStandFile(contentValues:ContentValues){
        contentValues.put(MediaStore.Images.Media.DESCRIPTION, "This is an image")
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "SimpleTest.png")
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/png")
        contentValues.put(MediaStore.Images.Media.TITLE, "SimpleTest.png")
//        contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "kkk/cxm")
        // MediaStore.Images.Media  MediaStore.Video  MediaStore.Audio.Media MediaStore.Files
        val insertUri = context?.contentResolver?.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI,contentValues)
        println("insertUri == $insertUri")
        insertUri?. let{
            context?.contentResolver?.openOutputStream(it)
        }?.let {
            val bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888)
            val tmpCanvas = Canvas()
            tmpCanvas.setBitmap(bitmap)
            tmpCanvas.drawColor(Color.GREEN)
            tmpCanvas.save()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)
            it.close()
            bitmap.recycle()
        }
    }


    private fun writeStandFile(contentValues:ContentValues){
        contentValues.put(MediaStore.Images.Media.DESCRIPTION, "This is an image")
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "SimpleTest.png")
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/png")
        contentValues.put(MediaStore.Images.Media.TITLE, "SimpleTest.png")

       // contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Camera/")
        //
        val insertUri = context?.contentResolver?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues)
        println("insertUri == $insertUri")
        insertUri?. let{
            context?.contentResolver?.openOutputStream(it)
        }?.let {
            val bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888)
            val tmpCanvas = Canvas()
            tmpCanvas.setBitmap(bitmap)
            tmpCanvas.drawColor(Color.GREEN)
            tmpCanvas.save()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)
            it.close()
            bitmap.recycle()
        }
    }
    // 查询所有的图片，查询非本应用创建的多媒体资源需要获取读权限
    @RequiresApi(Build.VERSION_CODES.O)
    private fun readStandFile() {
        val projection = arrayOf<String>(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA)
        val cursor = context?.contentResolver?.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null
        )
        cursor?.let {
            println("number == ${it.count}")
            it.moveToFirst()
            do {
                println("id == ${it.getString(it.getColumnIndex(MediaStore.Images.Media._ID))}")
                println("data == ${it.getString(it.getColumnIndex(MediaStore.Images.Media.DATA))}")
                println("DISPLAY_NAME == ${it.getString(it.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))}")
            } while (it.moveToNext())
        }
    }




}